/***

Accenture IDC     
Bhumika Udani     v1.0                              Allows workflow admin to clone the workflow                 
Bhumika Udani     v2.0     dt 14th October 2011     Adding comments
Sravanthi         v3.0     dt 2nd January 2012     Adding method level comments
**/

public with sharing class WorkflowCloneController 
{ 

  public list<stageWrapper> Stages{get;set;}
  public string asgnGrp{get;set;}
  public list<String> selectedStageNames{get;set;} 
  public list<String> selectedActiveStageNames{get;set;} 
  public List<wrapperOut> wrapout{get;set;}
  public List<AGWrapper> asgnGroups{get;set;}
  public string functionParam{get;set;}

  list<stage_definition__c> selectedStages = new list<stage_definition__c>();
  list<Workflow_Stage__c> selStages;
  list<AGWrapper> selectedGroups ;
  list<WF_Assignment_Group__c> WFGroupList;  

  Workflow_definition__c WF; 
  Workflow_definition__c NewWF; 
  boolean flag = true; 
  Map<ID,List<Activities_definition__c>> StepActMAP;  
  Map<string,id> funcNameMap =  new Map<string,id>(); 
  Map <String,List<Decimal>> sequenceStepMap;  
      
    //Contructor
    public WorkflowCloneController(ApexPages.StandardController controller) {
        WF = (Workflow_definition__c)controller.getRecord();
        /*TO display Pre-Selected stages and steps*/
        selectStages(); 
        selectedSteps();

    }
      Class AGWrapper{
        public Assignment_Group_Name__c AGroup{get;set;}
        public boolean selectbox{get;set;}
        public string functionName{get;set;}        
        public AGWrapper(Assignment_Group_Name__c G, boolean chkbox, string func){
            AGroup=G;
            selectbox =chkbox;
            functionName = func;
        }
     }
    //Wrapper classes to bind the selectboxes with the records                
        
    Class StepWrapper{
        public Step_Definition__c StepDef{get;set;}
        public boolean selbox{get;set;} 
        public string defaultAG{get;set;}
        public List<SelectOption> funcGroups{get;set;}       
        public stepWrapper(Step_Definition__c S, boolean chkbox){
            StepDef =S;
            selbox =chkbox;
        }
        public stepWrapper(){}
        public stepWrapper(step_definition__c S, boolean chkbox, string defaultGrp,List<SelectOption> funcGrps ){
            StepDef =S;
            selbox =chkbox;
            defaultAG = defaultGrp;
            funcGroups = funcGrps ;
        }
    }
    
    Class ActWrapper{
        public Activities_definition__c AD{get;set;}
        public boolean slctbox{get;set;}        
        public ActWrapper(Activities_definition__c A, boolean chkbox){
            AD =A;
            slctbox =chkbox;
        }
        public ActWrapper(){}
    }
    
    Class WrapperOut{
        public List<ActWrapper> Act{get;set;}
        public stepWrapper step{get;set;}        
        public wrapperOut(){
            if(step==null){step= new stepWrapper();}
            if(Act==null){Act= new List<ActWrapper>();}           
        }
    }                   
        
    Class stageWrapper{
        public stage_definition__c SD{get;set;}
        public boolean selectbox{get;set;}        
        public stageWrapper(stage_definition__c S, boolean chkbox){
            SD =S;
            selectbox =chkbox;
        }
    } 
   /* 1. Display the already exisiting functions of the parent workflow and add/remove existing funtions.
      2. allow to change sequence, validate unique sequence selection*/
    
   public void selectStages(){
       Stages =  new list<stageWrapper>();
       selectedStageNames = new list<string>();  
       selectedActiveStageNames = new list<string>();     
       set<id> stageIds = new set<id>();
       Map<ID,Decimal> stageSequenceMap = new Map<ID,Decimal>();

       selStages = [select name ,Stage__c,Stage__r.name,sequence__C from Workflow_Stage__c where workflow_code__c = :WF.Id order by sequence__C];
       List<Stage_definition__c> actStages = [select id,name ,sequence__C from stage_definition__c where status__c = 'active' order by sequence__C];
        for(Workflow_Stage__c ST :selStages){
            selectedStageNames.add(ST.Stage__r.name);
            stageIds.add(ST.Stage__c); 
            stageSequenceMap.put(ST.Stage__c,ST.sequence__C);
        }  
        for(Stage_definition__c S :actStages){
           if(flag){
            if(stageSequenceMap.containsKey(S.id)) {
                S.sequence__C=stageSequenceMap.get(S.id);
                Stages.add(new stageWrapper(S, true));
                selectedStages.add(S);
                selectedActiveStageNames.add(s.name);
                } 
            else  Stages.add(new stageWrapper(S, false));
            }       
        }
     }
    
    
   
    
    
    /* 1. Display exisiting tasks & allow to add/remove any other active tasks.
       2. Validate unique sequence allocation to each task. */    
    public void selectedSteps()
    {    
        getAssignmentGroup();
        getTaskAndActivites();
    }
    
    private void saveSelectedAssignmentGroup(){
       selectedGroups = new List<AGWrapper>(); 
       functionParam = '';    
       for(AGWrapper A: asgnGroups)
       {
           if(A.selectbox) { functionParam = functionParam + A.AGroup.Name; selectedGroups.add(A);}
       }
    }
    
    public pagereference saveGroup()
    {
        saveSelectedAssignmentGroup();
        if(validateAssignmentGroup())
        {  
            getTaskAndActivites();
        }
        else
        {  
              ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, IfullfillTextConstant.ASSIGNGRP_REQD_ERROR_MSG);          
              ApexPages.addMessage(msg); 
              if(wrapout != null) 
                wrapout.clear();
        }
        return null;    
    }   
  
    /*
    * This method save workflow definiation.
    */  
    private void insertWorkflowDefiniation(){
      try{         
         NewWF = new Workflow_definition__c();
         NewWF.Workflow_Name__c = WF.Workflow_Name__c;
         NewWF.Status__c = IfullfillTextConstant.WF_DEF_BUILDING_IN_PROGRESS_STATUS;
         NewWF.Workflow_Description__c = WF.Workflow_Description__c;
         NewWF.Version__c = WF.Version__c;
         insert NewWF; 
        }Catch(DMLException e){GeneralConstants.mailAnyExceptionToMe('WorkFLowCOntroller','SaveSteps: Insert Stages failed',e);}
    }
    /*
    * THis method save the workflow stages.
    */
    private void insertWorkflowStages(){
        list<Workflow_Stage__c> WFStageList = new list<Workflow_Stage__c>();
        for(stage_definition__c S:selectedStages)
        {        
                Workflow_Stage__c WFStage = new Workflow_Stage__c();
                WFStage.Workflow_Code__c = NewWF.id;
                WFStage.Name = S.name;
                WFStage.Stage__c = S.id;                
                WFStage.sequence__C = S.sequence__C;
                WFStageList.add(WFStage); 
        }     
        if(WFStageList.size()>0)
        {
            try{insert WFStageList;}
            Catch(DMLException e){GeneralConstants.mailAnyExceptionToMe('WorkFLowCOntroller','SaveSteps: Insert Stages failed',e);
            } 
        }        
    }
    /*
    * This method save workflow assignment group for the task.
    */
    private void inserWFGroups(){
    
        WFGroupList = new list<WF_Assignment_Group__c>();   
        Map<string,id> stageNameMap =  new Map<string,id>();
        list<Workflow_Stage__c> StageId = [select id, stage__c, name from Workflow_Stage__c where Workflow_Code__c = :NewWF.id];
        for(Workflow_Stage__c s: StageId)
        {
             stageNameMap.put(s.name, s.id);
        }       
            
         saveSelectedAssignmentGroup();
         System.debug('2222'+selectedGroups);
         
         for(AGWrapper grp :selectedGroups)
         {
                WF_Assignment_Group__c WFgrp = new WF_Assignment_Group__c();
                WFgrp.Name = grp.AGroup.Name;
                WFgrp.AG_Definition__c = grp.AGroup.id;
                WFgrp .Workflow_Function__c = stageNameMap.get(grp.functionName);
                WFGroupList.add(WFgrp); 
          }
          system.debug('WFGroupList ..'+WFGroupList+ 'first value '+WFGroupList[0].id);
          if (WFGroupList.size()>0 && WFGroupList[0].id == null)
          {
              system.debug('inside****');
             try{insert WFGroupList;}
             catch(dmlexception e){GeneralConstants.mailAnyExceptionToMe('WorkFLowCOntroller','SaveGroup: Insert Groups failed',e);}
          } 
    
    }
    /*
    *This method save the workflow Tasks.
    */
    private boolean insertWFTasks(){
    
        set<string> uniqueSeq = new set<string>();
        sequenceStepMap= new Map <String,List<Decimal>>();
        list<Wokflow_Steps__c> WFStepList = new list<Wokflow_Steps__c>();
        map<id,id> stageMap = new Map<id,id>();
        list<Workflow_Stage__c> StageIds = [select id, stage__c from Workflow_Stage__c where Workflow_Code__c = :NewWF.id];
        for(Workflow_Stage__c s: StageIds){
            stageMap.put(s.stage__c,s.id);
        } 
        for(integer i=0 ; i< wrapOut.size(); i++)
        {
            if(wrapOut[i].Step.selbox)
            {
            
                system.debug('******'+wrapOut[i].Step.StepDef.name);
                
                 if(sequenceStepMap.containsKey(wrapOut[i].Step.StepDef.stage__c)){
                      sequenceStepMap.get(wrapOut[i].Step.StepDef.stage__c).add(wrapOut[i].Step.StepDef.step_sequence__C);
                  }else{
                      sequenceStepMap.put(wrapOut[i].Step.StepDef.stage__c,new List<Decimal>{wrapOut[i].Step.StepDef.step_sequence__C});
                  } 
                  System.debug('function'+wrapOut[i].Step.StepDef.stage__c+'sequence'+wrapOut[i].Step.StepDef.step_sequence__C);

                 if(!uniqueSeq.contains(wrapOut[i].Step.StepDef.stage__c + String.valueof(Integer.valueof(wrapOut[i].Step.StepDef.step_sequence__C))))
                 {
                    uniqueSeq.add(wrapOut[i].Step.StepDef.stage__c + String.valueof(Integer.valueof(wrapOut[i].Step.StepDef.step_sequence__C)));
                    Wokflow_Steps__c WFStep = new Wokflow_Steps__c();
                    WFStep.Wokflow_Stage_Name__c = stageMap.get(wrapOut[i].Step.StepDef.stage__c);
                    WFStep.Name = wrapOut[i].Step.StepDef.name;
                    WFStep.Step__c = wrapOut[i].Step.StepDef.id;
                    WFStep.sequence__C = (wrapOut[i].Step.StepDef.step_sequence__C);
                    WFStep.Target_Days__c = wrapOut[i].Step.StepDef.Target_Days__C;
                    WFStep.Dashboard_Function__c = wrapOut[i].Step.StepDef.Dashboard_Function__C;
                    WFStep.Default_Group__c =  wrapOut[i].Step.defaultAG ;
                    WFStepList.add(WFStep);
                }
                else
                {
                      WFStepList.clear();
                      ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,IfullfillTextConstant.DUP_TASKSEQ_ERROR_MSG);
                      ApexPages.addMessage(msg);
                      return true;
                         
                }
            }
        }
        if(validateStepSequenceOrder()){ 
             ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, IfullfillTextConstant.TASKSEQ_SKIP_ERROR_MSG);
             ApexPages.addMessage(msg);    
             return true;                      
        }
       
        if(WFStepList.size()>0){
            try{Insert WFStepList;}
            catch(dmlException e){GeneralConstants.mailAnyExceptionToMe('WorkFLowController','SaveSteps: Insert Steps failed',e);
            }
        } 
       return false; 
    }
    /*
    *This method insert workflow Activities.
    */
    private void insertWFActivities(){    
        list<Workflow_Activities__c> WFActList = new list<Workflow_Activities__c>();
        map<id,id> stepMap = new Map<id,id>();
        list<Wokflow_Steps__c> StepIds = [select id, step__c from Wokflow_Steps__c where Wokflow_Stage_Name__r.Workflow_Code__c = :NewWF.id];
        for(Wokflow_Steps__c  s: StepIds ){
            stepMap.put(s.step__c,s.id);
        } 
        for(integer i=0 ; i< wrapOut.size(); i++){
         for(integer j=0 ; j< wrapOut[i].Act.size(); j++){
            if(wrapOut[i].Act[j].slctbox){
                Workflow_Activities__c WFAct = new Workflow_Activities__c();
                WFAct.Step_Name__c = stepMap.get(wrapOut[i].Act[j].AD.step__c);
                WFAct.Name = wrapOut[i].Act[j].AD.name;
                WFAct.Activity__c = wrapOut[i].Act[j].AD.id;
                WFAct.required__C = boolean.valueof(wrapOut[i].Act[j].AD.Required__C);
                WFActList .add(WFAct);
            }
          }
        }       
        if(WFActList.size()>0){
            try{Insert WFActList;}
            catch(dmlException e){GeneralConstants.mailAnyExceptionToMe('WorkFLowController','SaveSteps: Insert Steps failed',e);
            }
        } 
     }
    /*
    * This method include all saving functionality.
    */ 
    public pagereference  saveUpdatedWFDefn()
    {
        // Check if user has selected atleast one task per function 
        if(validateTask())
        {
             ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Select at least once task per function');
             ApexPages.addMessage(msg);
            return null;
        }
    
       //Save Workflow 
       insertWorkflowDefiniation();
       //Save Stages
       insertWorkflowStages();
       // Save Groups
       inserWFGroups();                   
        //Insert Tasks
       if(insertWFTasks())  return null;       
        //Insert Activities
       insertWFActivities();
       Pagereference Page = new Pagereference('/apex/WorkflowdefinitionSummaryView?id='+NewWF.id);     
       iFulfillHandler.updateLastTaskAndFunction(NewWF.Id);       
       return Page;
    } 
   /*
    * This method will save the workflow functions.
    */
    
     public pagereference saveFunctions(){
        flag = false;
        selectedStageNames.Clear();
        selectedStages.Clear();
        if(wrapout!=null) 
            wrapout.clear();
        if (asgnGroups!=null) asgnGroups.clear();
        for(integer i=0 ; i< Stages.size(); i++){
            if(Stages[i].selectBox){
                selectedStageNames.add(Stages[i].SD.name);
                selectedStages.add(Stages[i].SD);                 
            }
        }
        if(!validateStage())
        {
            selectedStageNames.Clear();
            selectedStages.Clear();
        return null;
        }
                
        //selectedSteps();
        getAssignmentGroup();
        if(wrapout!=null) wrapout.clear();
        return null;
    }
   /*
    * This method will validate the workflow Function sequence order.
    */ 
    private boolean validateStageSequenceOrder()
    {      
        system.debug('size ****'+selectedStages);
          Decimal maximumSequence =0.0;          
          for(stage_definition__c S:selectedStages){
           if(S.Sequence__c >maximumSequence){
               maximumSequence =S.Sequence__c;
           }            
          }          
          if(maximumSequence.intValue() !=  selectedStages.size()){
             return true;
          }  
          return false; 
    
    } 
    
    /*
    * This method will validate the workflow step sequence order.
    */
    private boolean validateStepSequenceOrder(){
    
           for(stage_definition__c S:selectedStages){
              List<Decimal> stepList = new List<Decimal>();
              if(sequenceStepMap.containsKey(S.id)){
               stepList = sequenceStepMap.get(S.id);
               if(stepList.size()>0){
                   stepList.sort();
                   if(stepList.size() !=stepList[stepList.size()-1].intValue()){
                       return true;
                   }
                }
               }     
            }            
        return false;
   }
     /*
      *  Check user has selected atleast one task per function
      *
      **/
     private boolean validateTask()
     {
         boolean status = false;
         Set<ID> selectedStageName = new Set<ID>();
         Set<ID> selectedStage1 = new Set<ID>();
         
         for(integer i=0 ; i< selectedStages.size(); i++)
         {
                selectedStageName.add(selectedStages[i].id); 
         }
         for(integer i=0 ; i< wrapOut.size(); i++)
         {
                if(wrapOut[i].Step.selbox)
                {
                    selectedStage1.add(wrapOut[i].Step.StepDef.stage__c);
                }
          }         
          if(selectedStageName.size() != selectedStage1.size())
          {
              status = true;
          }
          return status;
     }

   /*
    *  Check user has selected atleast one Assignment group per function
    *
    **/ 
    private boolean validateAssignmentGroup ()
    {
         boolean status = true ;
         Integer count = 0;
         Set<String> selectedStageName = new Set<String>();
         for(integer i=0 ; i< selectedStages.size(); i++)
         {
                selectedStageName.add(selectedStages[i].Name);           
         }
         for(String  strObj : selectedStageName)
         {
               count = 0;
               
               for(AGWrapper agwrapperObj: asgnGroups )
               {
                    // Check if user has selected check box and for each function
                     if(agwrapperObj.selectbox&& strObj ==agwrapperObj.functionName )
                     {
                         count++;
                     }
               }
               
               //  if user has not selected any assignment group for the func exit the loop
               if(count == 0)
               {
                   status  = false;
                   break;                         
               }                
         }   
         return status;
      }
    /*
     *   Validate the stage 
     *
     **/
    private  boolean validateStage()
    {
        boolean status = false;
        set<integer> uniqueSequence = new set<integer>(); 
        if(selectedStages.size() == 0)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please select at least one Function.');
            ApexPages.addMessage(msg);
            return status;
        }
        for(integer i=0 ; i< Stages.size(); i++)
        {
               if(Stages[i].selectBox)
               {
                     Stages[i].SD.Sequence__c = Math.ceil(Math.abs(Integer.valueof(Stages[i].SD.sequence__c)));
                    if(!uniqueSequence.contains(Integer.valueof(Stages[i].SD.sequence__c)))
                    {
                        uniqueSequence.add(Integer.valueof(Stages[i].SD.sequence__c));
                    }
                    else
                    {
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, IfullfillTextConstant.DUP_FUNCSEQ_ERROR_MSG );
                        ApexPages.addMessage(msg);                       
                        return status;
                    }                    
               }
        }
        // Check funcation sequence order
        if(validateStageSequenceOrder())
        {                    
             ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,IfullfillTextConstant.FUNCSEQ_SKIP_ERROR_MSG);
             ApexPages.addMessage(msg);   
             return status;       
        }
        return true;
    
    }
    
    /*
     *  Get the selected assignment group 
     *  
     **/
    private void getAssignmentGroup()
    {
         Map<ID, Workflow_Stage__c> map1  = new Map<ID, Workflow_Stage__c>();
          Set<String> wfAssignmentGroup = new Set<String>();  
          Set<ID> stageIds = new Set<ID>();  
              
          asgnGroups = new list<AGWrapper>(); 
           for(Workflow_Stage__c obj : selStages){
               map1.put(obj.Stage__c, obj);
              stageIds.add(obj.id);
           } 
              List<WF_Assignment_Group__c> wfAssinmentGroup = [Select  AG_Definition__c , Workflow_Function__c from WF_Assignment_Group__c where Workflow_Function__C  in :stageIds ];
                  
           for(WF_Assignment_Group__c wfAssignGroupObj : wfAssinmentGroup  ){
               String s = wfAssignGroupObj.Workflow_Function__c+'::'+ wfAssignGroupObj.AG_Definition__c;
               wfAssignmentGroup.add(s );
           }       
           for(integer i=0 ; i< Stages.size(); i++)
           {
               if(Stages[i].selectBox)
               {
                        //selectedStages.add(Stages[i].SD);
                        funcNameMap.put(Stages[i].SD.name, Stages[i].SD.id);
                        list<Assignment_Group_Name__c> Groups  = [select name, id from Assignment_Group_Name__c where function__c  INCLUDES (:Stages[i].SD.Name) and type__c = :IfullfillTextConstant.ASSIGNMENT_GROUP_TYPE ];               

                        for(Assignment_Group_Name__c Grp:Groups)
                        {
                            if(map1.get(Stages[i].SD.id) != null && wfAssignmentGroup.contains( map1.get(Stages[i].SD.id).id+'::'+Grp.id))
                            {
                              asgnGroups.add(new AGWrapper(Grp, true,Stages[i].SD.Name));
                            }
                            else
                            {
                               asgnGroups.add(new AGWrapper(Grp, false,Stages[i].SD.Name));
                            }
                        }
                }  
    
            }
      }
        
    /*
     *  Get the selected Task and activities group 
     *  
     **/    
    public void getTaskAndActivites()
    { 
           wrapout = new list<wrapperOut>();
           set<id> stepIds = new set<id>();
           set<id> stageIds = new set<id>();  
           Map<id,id> groupIds = new Map<id,id>();
           Map<id,boolean> stepActivityMap = new Map<id,boolean>();
           set<integer> uniqueSequence = new set<integer>(); 
           Set<String> wfAssignmentGroup = new Set<String>();          
           StepActMAP = new Map<ID,List<Activities_definition__c>>(); 
           List<Wokflow_Steps__c> selStepList = [select id,Step__c,Default_Group__c,name,Wokflow_Stage_Name__r.name,Sequence__c,Target_Days__c,Dashboard_Function__c from Wokflow_Steps__c where Wokflow_Stage_Name__c in :selStages];        

           List<Workflow_Activities__c> selActList = [select id,name,Activity__c,Activity_Description__c, status__c,required__C,Step_Name__r.name,Step_Name__c,Step_Name__r.Wokflow_Stage_Name__r.name,Step_Name__r.Target_Days__c,Step_Name__r.Dashboard_Function__c from Workflow_Activities__c where Step_Name__c in :selStepList];           
                           
           List<Step_Definition__c> StepList = [select id,name,step_definition__c.Stage__r.name,Step_Sequence__c,Target_Days__c,Dashboard_Function__c from step_definition__c where status__c='Active' and stage__c in :selectedStages order by stage__c];        
           Map<ID, Workflow_Stage__c> map1  = new Map<ID, Workflow_Stage__c>();
           Map<ID,Wokflow_Steps__c> selectedWFStepMap = new Map<ID,Wokflow_Steps__c>();
           
           for(Workflow_Stage__c obj : selStages){
               map1.put(obj.Stage__c, obj);
               stageIds.add(obj.id);
           }
           
           for(Wokflow_Steps__c ST :selStepList ){
               stepIds.add(ST.Step__c);
               selectedWFStepMap.put(ST.Step__c,ST);
               groupIds.put(ST.Step__c,ST.Default_Group__c);
           }
           //Display activities for each selected task and allow add/remove of activites
           
           for(Workflow_Activities__c AL :selActList ){
               stepActivityMap.put(AL.Activity__c,AL.required__C);
           }   
  
           for(Activities_definition__c ActList : [select id,name,Activity_Description__c, status__c,required__C,step__r.name,step__c,step__r.Stage__r.name,step__r.Target_Days__c,step__r.Dashboard_Function__c from Activities_definition__c where status__c = 'Active' and step__c in :StepList]){
                if(StepActMAP.containsKey(ActList.step__c)){
                    StepActMAP.get(ActList.step__c).add(ActList);//adds activities for this step to the activity list in the map    
                }else{
                    StepActMAP.put(ActList.step__c,new List<Activities_definition__c>{ActList});//adds new activity list for this step to the map    
                }                            
            }
           system.debug('asgnGroups ***'+asgnGroups);
          if(validateStageSequenceOrder()){                    
             ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,IfullfillTextConstant.FUNCSEQ_SKIP_ERROR_MSG);
             ApexPages.addMessage(msg);   
              selectedStageNames.Clear();
             selectedStages.Clear();
             return;       
          }
          for(Step_Definition__c stdef :StepList){
                wrapperOut tmpwrapper = new wrapperOut();
                list<SelectOption> tskGrpOptions = new list<SelectOption>();
                for(AGWrapper Gp :asgnGroups)
                {
                  if(stdef.Stage__r.name == Gp.functionName && Gp.selectbox){
                           tskGrpOptions.add(new SelectOption(Gp.AGroup.id,Gp.AGroup.Name)); 
                   }  
                     
                }  
                if(selectedWFStepMap.containsKey(stdef.id)){ 
                    Wokflow_Steps__c wfStep= selectedWFStepMap.get(stdef.id);
                    stdef.Step_Sequence__c=wfStep.Sequence__c;
                    stdef.Target_Days__c=wfStep.Target_Days__c;
                    stdef.Dashboard_Function__c=wfStep.Dashboard_Function__c;
                    tmpwrapper.Step=new stepWrapper(stdef, true,groupIds.get(stdef.id),tskGrpOptions);                    
                 }   
                else
                    tmpwrapper.Step=new stepWrapper(stdef, false,'--None--',tskGrpOptions);
                                    
                List<ActWrapper> twrap2=new List<ActWrapper>(); 
                if(StepActMAP.containsKey(stdef.id)){
                    for(Activities_definition__c ActDef : StepActMAP.get(stdef.id)){
                        if(stepActivityMap.containsKey(ActDef.id)){
                            ActDef.required__C = stepActivityMap.get(ActDef.id);
                            twrap2.add(new ActWrapper(ActDef, True));
                         }else
                            twrap2.add(new ActWrapper(ActDef, False));
                    }
                }
                tmpwrapper.Act = twrap2 ;                
                wrapout.add(tmpwrapper);
           }
           system.debug('step &&&'+wrapout);
    }
 }